<template>
  <div class="app-container">
    <!--查询表单-->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-input
          v-model="searchObj.hosname"
          placeholder="医院名称"
          @keyup.enter.native="fetchData()"
        />
      </el-form-item>

      <el-form-item>
        <el-input
          v-model="searchObj.hoscode"
          placeholder="医院编号"
          @keyup.enter.native="fetchData()"
        />
      </el-form-item>

      <el-button type="primary" icon="el-icon-search" @click="fetchData()"
        >查询</el-button
      >
      <el-button type="default" @click="resetData()">清空</el-button>

      <el-button type="danger" @click="batchRemove()">批量删除</el-button>
    </el-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="数据加载中"
      border
      fit
      highlight-current-row
      @selection-change="selectionChange"
    >

    <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column label="序号" width="70" align="center">
        <template slot-scope="scope">
          {{ (page - 1) * size + scope.$index + 1 }}
        </template>
      </el-table-column>

      

      <el-table-column prop="hosname" label="医院名称" width="170" />

      <el-table-column prop="hoscode" label="医院编号" width="160" />

      <el-table-column prop="apiUrl" label="地址" width="160" />

      <el-table-column prop="contactsName" label="联系人" />

      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          {{ scope.row.status === 1 ? "可用" : "不可用" }}
        </template>
      </el-table-column>

      <el-table-column label="操作" width="260" align="center">
        <template slot-scope="scope">
          <router-link :to="'/yygh/hospset/edit/' + scope.row.id">
            <el-button type="primary" size="mini" icon="el-icon-edit"
              >修改</el-button
            >
          </router-link>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="removeDataById(scope.row.id)"
            >删除</el-button
          >

          <el-button
            v-if="scope.row.status==0"
            type="warning"
            size="mini"
            icon="el-icon-unlock"
            @click="lock(scope.row.id,1)"
            >解锁</el-button
          >

          <el-button
            v-else
            type="warning"
            size="mini"
            icon="el-icon-lock"
            @click="lock(scope.row.id,0)"
            >锁定</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      :current-page="page"
      :page-size="size"
      :total="total"
      style="padding: 30px 0; text-align: center"
      layout="total, prev, pager, next, jumper"
      @current-change="fetchData"
    />
  </div>
</template>

<script>
import hosp from "@/api/hosp";

export default {
  data() {
    return {
      listLoading: false, //加载层
      list: [], //数据列表
      total: 0, //总行数
      page: 1,
      size: 5,
      searchObj: {}, //查询条件
      idList:[],//批量删除的ID
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    fetchData(page = 1) {
      //为接收到参数时使用默认值1
      this.page = page;
      this.listLoading = true;
      hosp.getList(this.page, this.size, this.searchObj).then((response) => {
        this.list = response.data.rows;
        this.total = response.data.total;

        this.listLoading = false;
      });
    },
    //清空条件
    resetData() {
      this.searchObj = {};
      this.fetchData();
    },
    //根据ID删除
    removeDataById(id) {
      //提示是否确定删除
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //判断当前页面是否只有一行
          let length = document.getElementsByClassName("el-table__row").length;
          if (length == 1) {
            this.page = this.page - 1;
          }
          hosp.removeById(id).then((response) => {
            this.$message.success("删除成功");
            this.fetchData(this.page);
          });
        })
        .catch(() => {});
    },

    //切换多选框
    selectionChange(rows){
        //获取选中行的ID

        //每次添加清空
        this.idList=[];
        for(let i=0;i<rows.length;i++){
            const a=rows[i].id
            this.idList.push(a);
        }
        
        
    },
    //批量删除
    batchRemove(){

        //提示是否确定删除
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          
          hosp.batchRemove(this.idList).then((response) => {
            this.$message.success("删除成功");
            this.fetchData(this.page);
          });
        })
        .catch(() => {});
        
        

        
    },

    //解锁与锁定
    lock(id,status){

        hosp.lock(id,status).then((response) => {
            this.$message.success(status==1?"解锁成功":"解锁失败");
            this.fetchData(this.page);
          })
    }

  },
};
</script>
